export function getDigit(params) {
  let digit = String(Math.ceil(params)).length - 1
  return digit
}

export function getMax(data) {
  if (!Array.isArray(data))
    throw Error('data is not a function in getMax(data)')
  let tempData = []
  data.forEach((item) => {
    tempData.push(Math.abs(item))
  })

  let max = isNaN(Math.max(...tempData)) ? 10 : Math.max(...tempData)
  if (max <= 0) max = 0
  else if (max > 0) {
    let digit = getDigit(max)
    let intergerNum = Math.pow(10, digit) / 2
    max =
      intergerNum * 2 === max
        ? intergerNum * 2
        : (parseInt(max / intergerNum) + 1) * intergerNum
  }
  return max <= 5 ? 5 : max > 5 && max <= 10 ? 10 : max //避免出现小数坐标轴
  // return max
}

export function getMin(data) {
  if (!Array.isArray(data))
    throw Error('data is not a function in getMin(data)')
  let min = Math.min(...data)

  if (min < 0) {
    return true
  }

  return false
}

export function getStyle(el) {
  if (el) {
    if (window.getComputedStyle) {
      return window.getComputedStyle(el, null)
    }

    return el.currentStyle
  }
}

export function chartsSize(container, charts) {
  let wi = getStyle(container, 'width').width
  let hi = getStyle(container, 'height').height
  charts.resize({
    width: `${parseInt(wi, 10) - 60}px`,
    height: `${parseInt(hi, 10) - 20}px`,
  })
}

export function chartsSizeSet(container, charts) {
  chartsSize(container, charts)
}

export function add0(m) {
  return m < 10 ? '0' + m : m
}

export function getTime(startTime, num) {
  let time = new Date(startTime)
  let y = time.getFullYear()
  let m = time.getMonth() + 1
  let d = time.getDate()

  time = y + '/' + this.add0(m) + '/' + this.add0(d)
  let getTime = new Date(time).getTime() - num * 24 * 60 * 60 * 1000

  return getTime
}

export function getXAxis(data, type) {
  if (!Array.isArray(data))
    throw Error('data is not a function in getMin(data)')

  data.forEach((item, index) => {
    let time = new Date(item)
    let y = time.getFullYear()
    let m = time.getMonth() + 1
    let d = time.getDate()
    if (data.length > 10 || type === 'app') {
      data[index] = m + '.' + d
    } else {
      data[index] = y + '.' + m + '.' + d
    }
  })
}
export function getMonthXAxis(data) {
  if (!Array.isArray(data))
    throw Error('data is not a function in getMin(data)')

  data.forEach((item, index) => {
    if (item.split('-').length > 1) {
      item = item.replace(/-/g, '')
    }
    let y = item.substring(0, 4)
    let m = parseInt(item.substring(4, 6))
    data[index] = y + '.' + m
  })
}
